/**
 * 
 */
package com.apache.website.common;

import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * @author dyhou
 *
 */
public class WcmCoreClient {

	private static WcmCoreClient instance;
	private static final String serviceName = "websiteService";
	private Logger log = Logger.getLogger(WcmCoreClient.class);

	private WcmCoreClient() {
	}

	public static WcmCoreClient getInstance() {
		if (null == instance) {
			instance = new WcmCoreClient();
		}
		return (instance);
	}

	public ResultMsg saveManuscript(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "saveManuscript", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改信息状态
	 */
	public ResultMsg editInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "editManuscriptStatus", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改信息
	 */
	public Object findManuscripts(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "manuscriptList", map,
				getInterfaceRegister());
		log.info("findManuscripts=" + entity.getMessage());
		return entity.getEntity();
	}

	/**
	 * description:  删除信息
	 */
	public Object findChannels(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "channelList", map,
				getInterfaceRegister());
		log.info("findChannels=" + entity.getMessage());
		return entity.getEntity();
	}

	/**
	 * description:  查询信息列表
	 */
	public Object selectManuscriptInfo(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "manuscriptEntity", map,
				getInterfaceRegister());
		log.info("selectManuscriptInfo=" + entity.getMessage());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public Object selectChannelInfo(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "channelEntity", map,
				getInterfaceRegister());
		log.info("selectChannelInfo=" + entity.getMessage());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  获取稿件内容
	 */
	public String getManuscriptContent(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "manuscriptContent", map,
				getInterfaceRegister());
		log.info("getManuscriptContent=" + entity.getMessage());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return String.valueOf(entity.getEntity());
		}
		return "";
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String reqUrl = ToolsUtil.getInstance().getValueByKey("info_release_url");
		String infoUrl = reqUrl + "/wcm/service/doService.action";
		if (reqUrl.startsWith("https:")) {
			register.setCallType("https");
			register.setAddress(infoUrl);
		} else if (reqUrl.startsWith("http:")) {
			register.setCallType("http");
			register.setAddress(infoUrl);
		} else {
			String port = ToolsUtil.getInstance().getValueByKey("info_release_port");
			register.setAddress(reqUrl);
			register.setPort(port);
			register.setCallType("socket");
		}
		return register;
	}

	/**
	 * description:  按主键查询会员信息
	 */
	public Object getMemberInfo(Map map) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "memberEntity", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}
}
